import "dart:convert";

import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:ct_pa_majordomo/utils/storage.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class WechatShare extends StatefulWidget {
  const WechatShare({super.key});

  @override
  State<StatefulWidget> createState() => _WechatShareState();

}

class _WechatShareState extends State<WechatShare> {

  List _templateList = [];
  late Map _apartment;
  Map _lockInfo = {
    "roomName": "A109",
    "lockName": "A109",
  };

  Widget _selectBox() {
    return Container(
      padding: EdgeInsets.all(ThemeVars.paddingPage),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border: Border.all(color: ThemeVars.colorBorder),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text("模板选择"),
          Row(
            children: [
              Text("短信模板", style: TextStyle(color: ThemeVars.colorTextTer),),
              Icon(Icons.arrow_forward_ios, color: ThemeVars.colorTextTer, size: 16,),
            ],
          )
        ],
      ),
    );
  }

  Widget _review(Map template) {
    return Container(
      padding: EdgeInsets.all(ThemeVars.paddingPage),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        border: Border.all(color: ThemeVars.colorBorder),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text("分享内容", style: TextStyle(fontSize: 16),),
          SizedBox(height: 12,),
          DefaultTextStyle(style: TextStyle(fontSize: 12, color: ThemeVars.colorTextSec),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Text("您好：这里是泰寓",),
                Text("房间：${_apartment["name"]}${_lockInfo["lockName"]}",),
                Text("密码：5566456",),
                Text("有效期：${DateTools.formatYMD(DateTime.now(), format: "{y}-{m}-{s} {h}:{i}")} - ${DateTools.formatYMD(DateTime.now(), format: "{y}-{m}-{s} {h}:{i}")}",),
                Text("输入密码后请按#号键或其他开锁键结束。wifi密码：15880123456",),
              ],
            )
          )
        ],
      )

    );
  }

  @override
  void initState() {
    super.initState();
    String apartment = storage.getString("apartment") ?? "";
    setState(() {
      _apartment = jsonDecode(apartment);
    });
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "微信分享",
      child: Container(
        padding: EdgeInsets.all(ThemeVars.paddingPage),
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            _selectBox(),
            SizedBox(height: 20,),
            _review({}),
            SizedBox(height: 60,),
            Button(
              onPressed: () async {
                // EasyLoading.showToast("未实现");
                String text = "您好：这里是泰寓\n"
                    "房间：${_apartment["name"]}${_lockInfo["lockName"]}\n"
                    "密码：5566456\n"
                    "有效期：${DateTools.formatYMD(DateTime.now(), format: "{y}-{m}-{s} {h}:{i}")} - ${DateTools.formatYMD(DateTime.now(), format: "{y}-{m}-{s} {h}:{i}")}\n"
                    "输入密码后请按#号键或其他开锁键结束。wifi密码：15880123456";
                await Clipboard.setData(ClipboardData(text: text));
                EasyLoading.showToast("复制成功，请往微信分享");
              },
              child: Text("微信分享"),
            )
          ],
        ),
      ),
    );
  }

}